#include "stdafx.h"
#include "Army.h"

#include "Ray.h"

void Army::addUnit( Brick & brick, XMFLOAT3 pos, XMFLOAT4 orient )
{
	Unit u;
	u.Position = pos;
	u.Orientation = orient;

	u.reset(brick);
	mUnits.push_back(u);
}

Unit * Army::findUnitRayIntersection( const mmath::Ray & ray )
{
	float closestDist = std::numeric_limits<float>::infinity();
	Unit * closestUnit = NULL;
	for(uint i = 0; i < mUnits.size(); i++)
	{
		mmath::BoundingSphere unitBSphere = mUnits[i].getBoundingSphere();
		XMVECTOR spherePos = XMLoadFloat3(&unitBSphere.Center);
		float dist = ray.intersectSphere(spherePos, unitBSphere.Radius);

		if(dist < closestDist && dist >= 0)
		{
			closestDist = dist;
			closestUnit = &mUnits[i];
		}
	}
	return closestUnit;
}

void Army::update( float dt )
{
	for(uint i = 0; i < mUnits.size(); i++)
	{
		mUnits[i].update(dt);
	}
}
